package com.ymy.thinking.in.spring.bean.gc;

import com.ymy.thinking.in.spring.bean.factory.PersonFactory;
import com.ymy.thinking.in.spring.bean.factory.RewriteFinalizePersonFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.io.IOException;

/**
 * Bean 垃圾回收（GC）示例
 *
 * @author Ringo
 * @date 2021/8/27 22:52
 */
public class BeanGarbageCollectionDemo {

    public static void main(String[] args) throws IOException {
        // 创建 IoC 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册 Bean. BeanGarbageCollectionDemo 相当于加了 @Configuration 注解
        applicationContext.register(BeanGarbageCollectionDemo.class);
        // 开启 Spring 应用上下文
        applicationContext.refresh();

        // 关闭 Spring 应用上下文
        applicationContext.close();

        // 强制触发 GC
        System.gc();

        System.in.read();
    }


    @Bean
    public PersonFactory personFactory() {
        return new RewriteFinalizePersonFactory();
    }

}
